package com.zhang.teachingcommunity_spring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhang.teachingcommunity_spring.controller.resultType.CoursePage;
import com.zhang.teachingcommunity_spring.mapper.CourseMapper;
import com.zhang.teachingcommunity_spring.mapper.SubjectMapper;
import com.zhang.teachingcommunity_spring.mapper.UserMapper;
import com.zhang.teachingcommunity_spring.mapper.entity.Course;
import com.zhang.teachingcommunity_spring.mapper.entity.Subject;
import com.zhang.teachingcommunity_spring.mapper.entity.User;
import com.zhang.teachingcommunity_spring.service.CourseService;
import com.zhang.teachingcommunity_spring.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class courseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/searchCourse")
    private Result searchCourse(String courseName) {
        List<Course> courses = courseService.searchCourse(courseName);
        return Result.success("获取成功",courses);
    }
    @PostMapping("/insert")
    private Result insert(@RequestBody Course course) {
        courseMapper.insert(course);
        return Result.success("添加成功",true);
    }
    @PostMapping("/update")
    private Result update(@RequestBody Course course) {
        courseMapper.updateById(course);
        return Result.success("修改成功",true);
    }
    @GetMapping("/delete")
    private Result delete( Integer courseId) {
        courseMapper.deleteById(courseId);
        return Result.success("删除成功",true);
    }
    @PostMapping("/getCourseList")
    private Result getCourseList(@RequestBody CoursePage coursePage) {
        IPage<Course> moocList = courseService.getCourseList(coursePage.getCourse(), coursePage.getPage(),coursePage.getSort());
        moocList.getRecords().forEach(item -> {
            String teacherId = item.getTeacherId();
            Integer subjectId = item.getSubjectId();
            Subject subject = subjectMapper.selectById(subjectId);
            User user = userMapper.selectById(teacherId);
            item.setUser(user);
            item.setSubject(subject);
        });
        return Result.success("获取成功",moocList);
    }
    @GetMapping("/getDifferentSubjectInCount")
    private Result getDifferentSubjectInCount( ) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        List<Map<String, Object>> differentSubjectInCount = courseMapper.getDifferentSubjectInCount();
        return Result.success("获取成功",differentSubjectInCount);
    }
}
